package com.bilin.coin.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bilin.coin.R;
import com.bilin.coin.bean.CZTaskBean;
import com.bilin.coin.bean.CommonSuccessEvent;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.common.base.BaseFragment;
import com.bilin.coin.network.RetrofitConfig;
import com.bilin.coin.tools.Geter;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.GsonUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kt.com.bilin.coin.ad.AdManager;
import kt.com.bilin.coin.ad.FusionAdManager;

public class TaskFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.bt3)
    Button bt3;
    @BindView(R.id.bt2)
    Button bt2;
    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.tv11)
    TextView tv11;
    @BindView(R.id.tv22)
    TextView tv22;
    @BindView(R.id.tv33)
    TextView tv33;

    @BindView(R.id.tv111)
    TextView tv111;
    @BindView(R.id.tv222)
    TextView tv222;
    @BindView(R.id.tv333)
    TextView tv333;

    @BindView(R.id.tv1111)
    TextView tv1111;
    @BindView(R.id.tv2222)
    TextView tv2222;
    @BindView(R.id.tv3333)
    TextView tv3333;

    @BindView(R.id.bt3_1)
    Button bt3_1;
    @BindView(R.id.tv33_1)
    TextView tv33_1;
    @BindView(R.id.tv333_1)
    TextView tv333_1;
    @BindView(R.id.tv3333_1)
    TextView tv3333_1;

    @BindView(R.id.bt3_2)
    Button bt3_2;
    @BindView(R.id.tv33_2)
    TextView tv33_2;
    @BindView(R.id.tv333_2)
    TextView tv333_2;
    @BindView(R.id.tv3333_2)
    TextView tv3333_2;

    @BindView(R.id.sw_refresh)
    SwipeRefreshLayout sw_refresh;

    private String typeAddTime = "0";//0比邻券 1种子 2工分

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        initRefresh();
    }

    private void initRefresh() {
        //设置样式刷新显示的位置
        sw_refresh.setProgressViewOffset(true, -20, 100);
        sw_refresh.setOnRefreshListener(this);
        //解决滑动冲突
      /*  appbar_layout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset >= 0) {
                    sw_refresh.setEnabled(true);
                } else {
                    sw_refresh.setEnabled(false);

                }
            }
        });*/
        sw_refresh.setEnabled(true);
    }

    private void getData() {
        new Geter((BaseAppCompatActivity) mContext, false, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-ad-cz/getData";
            }

            @Override
            protected Map<String, String> fillParams() {
                return new HashMap<>();
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                CZTaskBean czTaskBean = GsonUtil.GsonToBean(dataJson, CZTaskBean.class);
                fillData(czTaskBean);
            }
        };
    }

    private void fillData(CZTaskBean czTaskBean) {
        bt3.setEnabled(!czTaskBean.isIsAdOver());
        bt3.setText(!czTaskBean.isIsAdOver() ? "去完成" : "已完成");

        bt3_1.setEnabled(!czTaskBean.isIsAdOverTwo());
        bt3_1.setText(!czTaskBean.isIsAdOverTwo() ? "去完成" : "已完成");

        bt3_2.setEnabled(!czTaskBean.isIsAdOverThree());
        bt3_2.setText(!czTaskBean.isIsAdOverThree() ? "去完成" : "已完成");
        if (czTaskBean.isHasKyc()) {
            tv11.setText("完成1/1");
            bt1.setEnabled(false);
            bt1.setText("已实名");
        } else {
            tv11.setText("完成0/1");
            bt1.setEnabled(true);
            bt1.setText("去实名");
        }
        tv111.setText(czTaskBean.getKycAwardStr1());
        tv1111.setText(czTaskBean.getKycAwardStr2());

        tv22.setText("累计邀请：" + czTaskBean.getDirectChildKycNum() + "");
        tv222.setText(czTaskBean.getChildKycAwardStr1());
        tv2222.setText(czTaskBean.getChildKycAwardStr2());

        tv33.setText("累计完成" + czTaskBean.getTimes() + "/" + czTaskBean.getPerDayTimes());
        tv333.setText(czTaskBean.getAdAwardStr1());
        tv3333.setText(czTaskBean.getAdAwardStr2());

        tv33_1.setText("累计完成" + czTaskBean.getTimesTwo() + "/" + czTaskBean.getPerDayTimesTwo());
        tv333_1.setText(czTaskBean.getAdAwardStr1Two());
        tv3333_1.setText(czTaskBean.getAdAwardStr2Two());

        tv33_2.setText("累计完成" + czTaskBean.getTimesThree() + "/" + czTaskBean.getPerDayTimesThree());
        tv333_2.setText(czTaskBean.getAdAwardStr1Three());
        tv3333_2.setText(czTaskBean.getAdAwardStr2Three());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task;
    }

    @OnClick({R.id.tv_task_desc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_task_desc:

                break;
        }
    }

    private void addOneAd() {
        new Poster((BaseAppCompatActivity) mContext, true, false, false) {
            @Override
            protected String fillUrl() {
                return "/api/bl-ad-cz/addTimes";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("type", typeAddTime);//0比邻券 1种子 2工分
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                getData();
            }
        };
    }

    @OnClick({R.id.bt3, R.id.bt3_1, R.id.bt3_2,
            R.id.bt2, R.id.bt1, R.id.tv_task_desc})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.bt1:
                EventBus.getDefault().post(new CommonSuccessEvent(CommonSuccessEvent.GOTOREAL));
                break;
            case R.id.bt2:
                EventBus.getDefault().post(new CommonSuccessEvent(CommonSuccessEvent.GOTOINVITE));
                break;
            case R.id.bt3:
                initAndPlayAd("0");
                break;
            case R.id.bt3_1:
                initAndPlayAd("1");
                break;
            case R.id.bt3_2:
                initAndPlayAd("2");
                break;
            case R.id.tv_task_desc:
                jumpArticle("5");
                break;
        }
    }

    private void initAndPlayAd(String type) {
        typeAddTime = type;
        AdManager.INSTANCE.loadRewardVideoAd(getActivity(), () -> {
            addOneAd();
            return null;
        });
    }

    private void jumpArticle(String id) {
        baseStartActivityWith("/common/WebPageLoading")
                .withString("url", RetrofitConfig.newsUrl + id)
                .withString("title", "任务说明").navigation();
    }

    @Override
    public void onRefresh() {
        getData();
        sw_refresh.setRefreshing(false);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 登录成功
     *
     * @param commonSuccessEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loginSuccessEvent(CommonSuccessEvent commonSuccessEvent) {
        if (commonSuccessEvent.getType() == CommonSuccessEvent.LoginSuccess) {
            getData();
        }
        EventBus.getDefault().removeStickyEvent(commonSuccessEvent);
    }
}
